package com.tongda.menu.activity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.tongda.core.common.Constants;
import com.tongda.core.common.KernalApplication;
import com.tongda.core.repository.MenuCartDao;
import com.tongda.core.service.BillService;
import com.tongda.core.service.UserService;
import com.tongda.core.utils.ActivityUtil;
import com.tongda.core.utils.DishUtil;
import com.tongda.core.vo.Dish;
import com.tongda.core.vo.DishesAvoidfood;
import com.tongda.core.vo.DishesSetDishes;
import com.tongda.core.vo.DishesSetDishesReplace;
import com.tongda.core.vo.DishesTaste;
import com.tongda.menu.R;
import com.tongda.menu.widget.FlowLinearLayout;
import com.tongda.menu.widget.FlowRadioGroup;

public class CartEditActivity extends BaseActivity {

	// 忌口选项数据
	private List<DishesAvoidfood> dishesAvoidfoods = new ArrayList<DishesAvoidfood>();
	// 口味选项数据
	private List<DishesTaste> dishesTastes=new ArrayList<DishesTaste>();
	// 类型 bill or dish
	private String type;
	// 确定 图标
	private Button commit_Textview;
	// 忌口text
	private TextView jikou_TextView;
	// 忌口id字符串（向后台提交用）
	private String dishesAvoidfoodIdArr;
	// 忌口名称串
	private String dishesAvoidfoodNameArr;
	// 口味text
	private TextView kouwei_TextView;
	// 口味id字符串（向后台提交用）
	private String dishesTastesIdArr;
	// 口味名称串
	private String dishesTastesNameArr;
	// 辣度text
	private TextView ladu_TextView;
	// 辣度结果
	private int ladu_value = 0;
	// 辣度显示文字
	private String[] ladu_showArr;
	// 备注文本编辑
	private EditText beizhu_editText;
	private Dish dish;
	private Intent intent;
	private MenuCartDao orderdish_dao;
	private TextView dishNameTv;
	private TextView billTitleTv;
	private KernalApplication application;
	private MenuCartDao order_dishdao;
	private DishUtil dishUtil;
	private LinearLayout dishPriceLy;
	private ImageView cancelImv;
	private EditText rpriceEdt;
	private boolean rulingFlag;
	private String billId;
	private LinearLayout dsReplaceLy;
	private FlowLinearLayout jikouRp;
	private FlowLinearLayout kouweiRp;
	private FlowRadioGroup laduRp;
	private String page;
	private String billNotes;
	private String billNotesStr;
	private Dish appBillNotes;
	private String punLevel;
	private LinearLayout mainRL;
	private TextView title_textView;
	private TextView xuancaiTv;
	private ImageView dividerImv;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.bill_detail_prbz);
		application = KernalApplication.getInstance();
		appBillNotes = application.getBillNotes();
		order_dishdao= MenuCartDao.getInstance(this);
		dishUtil = DishUtil.getInstance(this);
		
		intent = getIntent();
		type = intent.getStringExtra("type");
		dish = (Dish) intent.getSerializableExtra("dish");
		billId = intent.getStringExtra("billId");
		billNotes = intent.getStringExtra("billNotes");
		billNotesStr = intent.getStringExtra("billNotesStr");
		punLevel = intent.getStringExtra("punLevel");
		//主要看是不是从详情页面跳转过来的
		page = intent.getStringExtra("page");
		dishesAvoidfoods = new ArrayList<DishesAvoidfood>();
		dishesTastes = new ArrayList<DishesTaste>();
		
		initCurrentView();
		new LoadTask().execute();
		
		orderdish_dao = MenuCartDao.getInstance(this);
		
	}

	protected void initCurrentView() {
		mainRL = (LinearLayout) findViewById(R.id.bill_prbz_mainRL);
		title_textView = (TextView) findViewById(R.id.bill_prbz_title_text);
		commit_Textview = (Button) findViewById(R.id.bill_prbz_commit_text);
		billTitleTv = (TextView)findViewById(R.id.bill_prbz_title_text);
		dishNameTv = (TextView)findViewById(R.id.bill_prbz_dishName);
		dishPriceLy = (LinearLayout)findViewById(R.id.cartedit_dishpricely);
//		cancelImv = (ImageView)findViewById(R.id.bill_prbz_cancel_Imv);
		cancelImv = new ImageView(this);
		rpriceEdt = (EditText)findViewById(R.id.cartedit_dishrpriceEdt);
		jikouRp = (FlowLinearLayout)findViewById(R.id.bill_prbz_jikou_radiogroup);
		kouweiRp = (FlowLinearLayout)findViewById(R.id.bill_prbz_kouwei_radiogroup);
		laduRp = (FlowRadioGroup)findViewById(R.id.bill_prbz_ladu_radiogroup);
		xuancaiTv = (TextView)findViewById(R.id.bill_prbz_xuancai);
		dividerImv = (ImageView)findViewById(R.id.bill_prbz_divider);
		
		
		if(page!=null && page.equals("show")){
			findViewById(R.id.bill_prbz_jikou_ly).setVisibility(View.GONE);
			findViewById(R.id.bill_prbz_kouwei_ly).setVisibility(View.GONE);
			findViewById(R.id.bill_prbz_ladu_ly).setVisibility(View.GONE);
			findViewById(R.id.bill_prbz_beizhu_RL).setVisibility(View.GONE);
		}
		
		if(type.equals("dish")){
			dishNameTv.setText(dish.getDishesName());
			if(dish.getIsSet().equals("true")){
				billTitleTv.setText("套餐编辑");
			}
	        /*
	         * 处理是否显示时价字样
	         * */
	        if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
	        	dishPriceLy.setVisibility(View.VISIBLE);
	        	rpriceEdt.setText(dish.getPrice()+"");
	        	
	        	rpriceEdt.setOnTouchListener(new OnTouchListener() {
	        		private String username;
	    			private String password;
	    			
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if(event.getAction()==MotionEvent.ACTION_DOWN){
							if(!rulingFlag){
								final AlertDialog dialog = new AlertDialog.Builder(CartEditActivity.this).create();
								View view = View.inflate(CartEditActivity.this, R.layout.dialog_verifyid, null);
								dialog.setView(view,0,0,0,0);
								dialog.setCanceledOnTouchOutside(false);
								dialog.show();
								final EditText ID_Edt = (EditText)view.findViewById(R.id.verifyid_ID_edt);
							    final EditText PD_Edt = (EditText)view.findViewById(R.id.verifyid_pw_edt);
								ImageView Cancel_Imv = (ImageView)view.findViewById(R.id.verifyid_cancle);
								Button LG_Btn = (Button)view.findViewById(R.id.verifyid_login_btn);
								
								LG_Btn.setOnClickListener(new OnClickListener() {

									private int result;

									@Override
									public void onClick(View v) {
										 username = ID_Edt.getText().toString();
										 password = PD_Edt.getText().toString();
										 
											Thread loginThread = new Thread(new Runnable() {
												
												@Override
												public void run() {
													result = UserService.login(CartEditActivity.this,username, password,false);
												}
											});
											
											
										if(!username.equals("") && !password.equals("")){
											loginThread.start();
											try {
												loginThread.join();
											} catch (InterruptedException e) {
												e.printStackTrace();
											}
				                            if(result==0){
												final Handler handler = new Handler(){

													@Override
													public void handleMessage(
															Message msg) {
														super.handleMessage(msg);
														if(msg.what==1){
															ActivityUtil.Toast(CartEditActivity.this, "身份验证成功", false, false);
															rulingFlag = true;
															rpriceEdt.setSelectAllOnFocus(true);
															dialog.dismiss();
														}else{
															ActivityUtil.Toast(CartEditActivity.this, "您没有点菜权限,无法修改时价菜", false, false);
															rpriceEdt.setSelection(0);
															dialog.dismiss();
														}
													}
													
												};
												Thread perMissionThread  = new Thread(new Runnable() {
													
													@Override
													public void run() {
														Message msg = handler.obtainMessage();
														if(UserService.getCurrentUserHasPermission("frontdesk_bill_diancai:create")){
															msg.what=1;
															handler.sendMessage(msg);
														}else{
															msg.what=0;
															handler.sendMessage(msg);
														}
													}
												});
												perMissionThread.start();
											}else{
												ActivityUtil.Toast(CartEditActivity.this, "身份验证失败", false, false);
											}
										}else{
											ActivityUtil.Toast(CartEditActivity.this, "用户名或密码为空", false, false);
										}
									}
								});
								Cancel_Imv.setOnClickListener(new OnClickListener() {
									
									@Override
									public void onClick(View v) {
										rpriceEdt.setSelection(0);
										dialog.dismiss();
									}
								});
								
							}
						}
						return false;
					}
				});
	        	
	        	rpriceEdt.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						String text = s.toString();
						if(text.contains(".") && text.length()-text.indexOf(".")>3){
							String result = text.substring(0, text.indexOf(".")+3);
							rpriceEdt.setText(result);
						}
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
					}
					
					@Override
					public void afterTextChanged(Editable s) {
					}
				});
	        }
		}else{
			dishNameTv.setVisibility(View.GONE);
			billTitleTv.setText("全单备注");
		}
		
	        /*
	         * 更新之后处理套餐以及可替换菜肴
	         * */
	        dsReplaceLy = (LinearLayout)findViewById(R.id.bill_prbz_dishesetReplaceLy);
    		LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    		lp0.setMargins(20, 0, 20, 20);
    		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    		lp.setMargins(20, 20, 20, 20);
    		
	        if(type.equals("dish") && dish.getIsSet().equals("true")&&dish.getDsDishes()!=null&&dish.getDsDishes().size()>0){
	        	xuancaiTv.setVisibility(View.VISIBLE);
	        	dividerImv.setVisibility(View.VISIBLE);
	        	
	        	final List<DishesSetDishes> dsDishes = dish.getDsDishes();
	        	if(dish.getIsDinerDish().equals("1")){
	        		Thread subThread = new Thread(new Runnable() {
						
						@Override
						public void run() {
			        		for(DishesSetDishes ds:dsDishes){
			        			List<DishesSetDishesReplace> dishesSetDishesReplaces = new ArrayList<DishesSetDishesReplace>();
			        			dishesSetDishesReplaces = BillService.getMenuReplaceDishes(dish.getDishSetId(), ds.getDsDishesId());
			        			
			   				 for(int i=0;i<dishesSetDishesReplaces.size();i++){
								 if(ds.getDishesId().equals(dishesSetDishesReplaces.get(i).getDishesId())){
									 dishesSetDishesReplaces.get(i).setDishesId( ds.getMr_dishesId());
									 dishesSetDishesReplaces.get(i).setDishesName(ds.getMr_dishesName());
									 dishesSetDishesReplaces.get(i).setDishesUnitName(ds.getMr_unitName());
									 dishesSetDishesReplaces.get(i).setDsUnitNum(new BigDecimal(ds.getMr_unitNum()));
									 dishesSetDishesReplaces.get(i).setDishesCode(ds.getMr_dishesCode());
										break;
								 }
							 }
								ds.setDishesSetDishesReplaces(dishesSetDishesReplaces);
			        		}
						}
					});
	        		subThread.start();
	        		try {
						subThread.join();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
	        	}
	        	for(int i =0;i<dsDishes.size();i++){
	        		FlowRadioGroup group = new FlowRadioGroup(this);
	        		if(i!=0){
	        			group.setLayoutParams(lp);
	        		}else{
	        			group.setLayoutParams(lp0);
	        		}
	        		group.setOrientation(0);
	        		RadioButton firstBtn = new RadioButton(this);
	        		final DishesSetDishes dsDishe = dsDishes.get(i);
	        		//虚拟出一个替换菜肴，代替0的位置以方便替换
	        		DishesSetDishesReplace firstrp = new DishesSetDishesReplace();
	        		String dsName = dsDishe.getDishesName();
					String dsId = dsDishe.getDishesId();
					String dsCode = dsDishe.getDishesCode();
					String dsNum = dsDishe.getUnitNum();
					String dsuName = dsDishe.getUnitName();
					
	        		firstrp.setDishesName(dsName);
	        		firstrp.setDishesId(dsId);
	        		firstrp.setDishesCode(dsCode);
	        		firstrp.setDsUnitNum(new BigDecimal(dsNum));
	        		firstrp.setDishesUnitName(dsuName);
	        		
//2.2.0	        		if(ActivityUtility.getDPI(CartEditActivity.this).equals("240")){
//	        			firstBtn.setPadding(15, 0, 0, 0);
//	        		}else if(ActivityUtility.getDPI(CartEditActivity.this).equals("160")){
//	        			firstBtn.setPadding(18, 0, 0, 0);
//	        		}
	        		
	        		firstBtn.setText(firstrp.getDishesName());
	        		firstBtn.setButtonDrawable(android.R.color.transparent); 
	        		firstBtn.setButtonDrawable(R.drawable.bg_singlechoose);
	        		firstBtn.setTag(firstrp);
	        		group.addView(firstBtn);
	        		group.check(firstBtn.getId());
	        		List<DishesSetDishesReplace>dsReplaces =dsDishe.getDishesSetDishesReplaces();
	        		for(int j=0;j<dsReplaces.size();j++){
	        			RadioButton rb = new RadioButton(this);
//	2.2.0	        		if(ActivityUtility.getDPI(CartEditActivity.this).equals("240")){
//		        			rb.setPadding(15, 0, 0, 0);
//		        		}else if(ActivityUtility.getDPI(CartEditActivity.this).equals("160")){
//		        			rb.setPadding(18, 0, 0, 0);
//		        		}
	        			rb.setText(dsReplaces.get(j).getDishesName());
	        			rb.setButtonDrawable(android.R.color.transparent); 
	        			rb.setButtonDrawable(R.drawable.bg_singlechoose);
	        			DishesSetDishesReplace replaceDish = 	dsReplaces.get(j);
	        			rb.setTag(replaceDish);
	        			group.addView(rb);
	        		}
	        		
	        		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						
						@Override
						public void onCheckedChanged(RadioGroup group, int checkedId) {
						
								for(int index=0;index<group.getChildCount();index++){
									if(group.getChildAt(index).getId()==checkedId){
											String dsName = dsDishe.getDishesName();
											String dsId = dsDishe.getDishesId();
											String dsCode = dsDishe.getDishesCode();
											String dsNum = dsDishe.getUnitNum();
											String dsuName = dsDishe.getUnitName();
											
											DishesSetDishesReplace dsreplace = (DishesSetDishesReplace)group.getChildAt(index).getTag();
											String rpName = dsreplace.getDishesName();
											String rpId = dsreplace.getDishesId();
											String rpCode = dsreplace.getDishesCode();
											String rpNum = String.valueOf(dsreplace.getDsUnitNum());
											String rpuName = dsreplace.getDishesUnitName();
											
											dsDishe.setDishesName(rpName);
											dsDishe.setDishesId(rpId);
											dsDishe.setDishesCode(rpCode);
											dsDishe.setUnitNum(rpNum);
											dsDishe.setUnitName(rpuName);
											
											dsreplace.setDishesName(dsName);
											dsreplace.setDishesId(dsId);
											dsreplace.setDishesCode(dsCode);
											dsreplace.setDsUnitNum(new BigDecimal(dsNum));
											dsreplace.setDishesUnitName(dsuName);
	
									}
								}
						}
					});
	        		dsReplaceLy.addView(group);
	        	}
	        }else{
	        	xuancaiTv.setVisibility(View.GONE);
	        	dividerImv.setVisibility(View.GONE);
	        }
	        
		jikou_TextView = (TextView) findViewById(R.id.bill_prbz_jikou_TextView);
		dishesAvoidfoodIdArr = "";
		dishesAvoidfoodNameArr = "";

		jikou_TextView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				
			}
		});
		
		
		kouwei_TextView = (TextView) findViewById(R.id.bill_prbz_kouwei_TextView);
		dishesTastesIdArr = "";
		dishesTastesNameArr = "";

		kouwei_TextView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				
			}
		});
		
		ladu_TextView = (TextView) findViewById(R.id.bill_prbz_ladu_TextView);
		ladu_showArr = new String[] { "默认", "不辣", "微辣", "中辣", "辣", "特辣" };
		ladu_value = 0;

		ladu_TextView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
			}
		});
		
		beizhu_editText = (EditText) findViewById(R.id.bill_prbz_beizhu_EditText);
		
		if(type.equals("dish")){
			beizhu_editText.setText(dish.getBillnotes());
		}else if(type.equals("bill")){
			if(billNotes!=null){
				beizhu_editText.setText(billNotes);
			}else if(appBillNotes!=null && appBillNotes.getBillnotes()!=null){
				beizhu_editText.setText(appBillNotes.getBillnotes());
			}
		
		}
	
		
		beizhu_editText.addTextChangedListener(new TextWatcher() {
		    private int maxLen = 15;  
		      
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
		        Editable editable = beizhu_editText.getText();  
		        int len = editable.length();  
		        if(len > maxLen)  
		        {  
		            int selEndIndex = Selection.getSelectionEnd(editable);  
		            String str = editable.toString();  
		            //截取新字符串  
		            String newStr = str.substring(0,maxLen);  
		            beizhu_editText.setText(newStr);  
		            editable = beizhu_editText.getText();  
		            //新字符串的长度  
		            int newLen = editable.length();  
		            //旧光标位置超过字符串长度  
		            if(selEndIndex > newLen)  
		            {  
		                selEndIndex = editable.length();  
		            }  
		            //设置新光标所在的位置  
		            Selection.setSelection(editable, selEndIndex);  
		            
		            
		            
		        }  
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
		
		cancelImv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CartEditActivity.this.finish();
			}
		});
	}


	public class LoadTask extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			dishesTastes = BillService.getAllDishesTaste();
			dishesAvoidfoods = BillService.getAllDishesAvoidfoods();
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if(dishesTastes==null){
				dishesTastes = new ArrayList<DishesTaste>();
				cancel(true);
				new LoadTask().execute();
			}
			if(dishesAvoidfoods==null){
				dishesAvoidfoods = new ArrayList<DishesAvoidfood>();
				cancel(true);
				new LoadTask().execute();
			}
			describeView();
		}
	}
	protected void describeView() {

		commit_Textview.setOnClickListener(new View.OnClickListener() {
			String beizhu;
			@Override
			public void onClick(View v) {
				//处理从详情页面过来的逻辑
				if(page!=null && page.equals("show")){
					Intent data = new Intent();
					data.putExtra("resultDish", dish);
					data.putExtra("result", "edit");
//					CartEditActivity.this.createPendingResult(2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
					setResult(RESULT_OK, data);
					CartEditActivity.this.finish();
				}
				
				 beizhu = beizhu_editText.getText().toString();
				if(type.equals("dish")){
					if(dish.getIsDinerDish().equals("0")){
						   if(dish.getIsRulingPrice()!=null && dish.getIsRulingPrice().equals("1")){
							   order_dishdao.updaterulingPrice(String.valueOf(dish.get_id()), rpriceEdt.getText().toString());
						   }
								orderdish_dao.update(String.valueOf(dish.get_id()), dishesTastesIdArr, dishesTastesNameArr, dishesAvoidfoodIdArr, dishesAvoidfoodNameArr, ladu_value, beizhu);
								String dsDishesStr = dishUtil.dsDishes2JsonStr(dish.getDsDishes());
								order_dishdao.updateOrdishesdsDishes(String.valueOf(dish.get_id()), dsDishesStr);
								CartEditActivity.this.finish();
					}else if(dish.getIsDinerDish().equals("1")){
						Thread subThread = new Thread(new Runnable() {
							
							@Override
							public void run() {
								//把菜肴的备注信息加入账单中
								if(dish.getIsSet().equals("false")){
									
									BillService.savePrbz("dish", dish.getDinerBillDisheId(), dishesTastesIdArr, dishesTastesNameArr, dishesAvoidfoodIdArr, dishesAvoidfoodNameArr, ladu_value, beizhu, "0");
								}else if(dish.getIsSet().equals("true")){
									//提交替换的菜肴
									String dsDishesDesc = DishUtil.beanToJson(dish.getDsDishes());
									BillService.editDishesSetReplace(dish.getDinerBillDisheId(), dsDishesDesc);
									
									BillService.savePrbz("dish", dish.getDinerBillDisheId(), dishesTastesIdArr, dishesTastesNameArr, dishesAvoidfoodIdArr, dishesAvoidfoodNameArr, ladu_value, beizhu, "1");
								}
							}
						});
						subThread.start(); 
						ActivityUtil.Toast(CartEditActivity.this, "正在保存信息......", false, false);
						while(subThread.isAlive()){
							if(!subThread.isAlive()){
								ActivityUtil.Toast(CartEditActivity.this, "保存成功", false, false);
								CartEditActivity.this.finish();
								break;
							}
						}
					}
				}else if(type.equals("bill")){
					if(billId!=null){
						Thread billThread = new Thread(new Runnable() {
							
							@Override
							public void run() {
								BillService.savePrbz("bill", billId, dishesTastesIdArr, dishesTastesNameArr, dishesAvoidfoodIdArr, dishesAvoidfoodNameArr, ladu_value, beizhu, "1");
							}
						});
						billThread.start();
						ActivityUtil.Toast(CartEditActivity.this, "正在保存备注信息......", false, false);
						CartEditActivity.this.finish();
						while(billThread.isAlive()){
							if(!billThread.isAlive()){
								ActivityUtil.Toast(CartEditActivity.this, "保存成功", false, false);
								break;
							}
						}
					}else{
						Dish billnotes = new Dish();
						billnotes.setTasteIdArray(dishesTastesIdArr);
						billnotes.setTasteNameArray(dishesTastesNameArr);
						billnotes.setAvoidfoodIdArray(dishesAvoidfoodIdArr);
						billnotes.setAvoidfoodNameArray(dishesAvoidfoodNameArr);
						billnotes.setLadu_value(ladu_value);
						billnotes.setBillnotes(beizhu);
						application.setBillNotes(billnotes);
						ActivityUtil.Toast(CartEditActivity.this, "保存成功", false, false);
						CartEditActivity.this.finish();
					}
				}
				
			}
		});
		//处理忌口
		if(dishesAvoidfoods.size()>0){
			jikouRp.setVisibility(View.VISIBLE);
			jikou_TextView.setVisibility(View.GONE);
			
			for(DishesAvoidfood avoidFood:dishesAvoidfoods){
				final CheckBox cb = new CheckBox(this);
// 2.2.0       		if(ActivityUtility.getDPI(CartEditActivity.this).equals("240")){
//        			cb.setPadding(15, 0, 0, 0);
//        		}else if(ActivityUtility.getDPI(CartEditActivity.this).equals("160")){
//        			cb.setPadding(18, 0, 0, 0);
//        		}
				cb.setText(avoidFood.getName()+"   ");
				cb.setTag(avoidFood);
				cb.setButtonDrawable(getResources().getDrawable(R.drawable.bg_multichoose));
				if(type.equals("dish")){
					if(dish!=null && dish.getAvoidfoodNameArray()!=null && dish.getAvoidfoodNameArray().contains(avoidFood.getName())){
						cb.setChecked(true);
						dishesAvoidfoodIdArr = dishesAvoidfoodIdArr + ((DishesAvoidfood) cb.getTag()).getCdaId() + ",";
						dishesAvoidfoodNameArr = dishesAvoidfoodNameArr + ((DishesAvoidfood) cb.getTag()).getName() + ",";
					}
				}else if(type.equals("bill")){
					if(billNotesStr!=null){
						if(billNotesStr.contains(avoidFood.getName())){
							cb.setChecked(true);
							dishesAvoidfoodIdArr = dishesAvoidfoodIdArr + ((DishesAvoidfood) cb.getTag()).getCdaId() + ",";
							dishesAvoidfoodNameArr = dishesAvoidfoodNameArr + ((DishesAvoidfood) cb.getTag()).getName() + ",";
						}
					}else if(appBillNotes!=null && appBillNotes.getAvoidfoodNameArray()!=null && appBillNotes.getAvoidfoodNameArray().contains(avoidFood.getName())){
						cb.setChecked(true);
						dishesAvoidfoodIdArr = dishesAvoidfoodIdArr + ((DishesAvoidfood) cb.getTag()).getCdaId() + ",";
						dishesAvoidfoodNameArr = dishesAvoidfoodNameArr + ((DishesAvoidfood) cb.getTag()).getName() + ",";
					}
				}

				
				jikouRp.addView(cb);
				
				cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						if(isChecked){
							dishesAvoidfoodIdArr = dishesAvoidfoodIdArr + ((DishesAvoidfood) cb.getTag()).getCdaId() + ",";
							dishesAvoidfoodNameArr = dishesAvoidfoodNameArr + ((DishesAvoidfood) cb.getTag()).getName() + ",";
						}else{
							dishesAvoidfoodIdArr = dishesAvoidfoodIdArr.replace(((DishesAvoidfood) cb.getTag()).getCdaId() + ",", "");
							dishesAvoidfoodNameArr = dishesAvoidfoodNameArr.replace(((DishesAvoidfood) cb.getTag()).getName()+",", "");
						}
					}
				});
				
			}
		}else{
			jikouRp.setVisibility(View.GONE);
			jikou_TextView.setVisibility(View.VISIBLE);
		}
		//处理口味
		if(dishesTastes.size()>0){
			kouweiRp.setVisibility(View.VISIBLE);
			kouwei_TextView.setVisibility(View.GONE);
			
			for(DishesTaste taste:dishesTastes){
				final CheckBox cb = new CheckBox(this);
//2.2.0        		if(ActivityUtility.getDPI(CartEditActivity.this).equals("240")){
//        			cb.setPadding(15, 0, 0, 0);
//        		}else if(ActivityUtility.getDPI(CartEditActivity.this).equals("160")){
//        			cb.setPadding(18, 0, 0, 0);
//        		}
				cb.setText(taste.getName()+"   ");
				cb.setTag(taste);
				cb.setButtonDrawable(getResources().getDrawable(R.drawable.bg_multichoose));
				if(type.equals("dish")){
				if(dish!=null && dish.getTasteNameArray()!=null && dish.getTasteNameArray().contains(taste.getName())){
					cb.setChecked(true);
					dishesTastesIdArr = dishesTastesIdArr + ((DishesTaste) cb.getTag()).getTasteId() + ",";
					dishesTastesNameArr = dishesTastesNameArr + ((DishesTaste) cb.getTag()).getName() + ",";
				}
				}else if(type.equals("bill")){
					if(billNotesStr!=null){
						if(billNotesStr.contains(taste.getName())){
							cb.setChecked(true);
							dishesTastesIdArr = dishesTastesIdArr + ((DishesTaste) cb.getTag()).getTasteId() + ",";
							dishesTastesNameArr = dishesTastesNameArr + ((DishesTaste) cb.getTag()).getName() + ",";
						}
					}else if(appBillNotes!=null && appBillNotes.getTasteNameArray()!=null && appBillNotes.getTasteNameArray().contains(taste.getName())){
						cb.setChecked(true);
						dishesTastesIdArr = dishesTastesIdArr + ((DishesTaste) cb.getTag()).getTasteId() + ",";
						dishesTastesNameArr = dishesTastesNameArr + ((DishesTaste) cb.getTag()).getName() + ",";
					}
				}
				
				kouweiRp.addView(cb);
				
				cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						if(isChecked){
							dishesTastesIdArr = dishesTastesIdArr + ((DishesTaste) cb.getTag()).getTasteId() + ",";
							dishesTastesNameArr = dishesTastesNameArr + ((DishesTaste) cb.getTag()).getName() + ",";
						}else{
							dishesTastesIdArr = dishesTastesIdArr.replace(((DishesTaste) cb.getTag()).getTasteId() + ",", "");
							dishesTastesNameArr = dishesTastesNameArr.replace(((DishesTaste) cb.getTag()).getName()+",", "");
						}
					}
				});
				
				
			}
		}else{
			kouweiRp.setVisibility(View.GONE);
			kouwei_TextView.setVisibility(View.VISIBLE);
		}
		
		for(int i=0;i<ladu_showArr.length;i++){
			String ladu = ladu_showArr[i];
			if(i==0){
				RadioButton firstRb = new RadioButton(this);
//2.2.0        		if(ActivityUtility.getDPI(CartEditActivity.this).equals("240")){
//        			firstRb.setPadding(15, 0, 0, 0);
//        		}else if(ActivityUtility.getDPI(CartEditActivity.this).equals("160")){
//        			firstRb.setPadding(18, 0, 0, 0);
//        		}
				firstRb.setText(ladu+"   ");
				firstRb.setButtonDrawable(getResources().getDrawable(R.drawable.bg_singlechoose));
				laduRp.addView(firstRb);
				laduRp.check(firstRb.getId());
			}else{
				RadioButton rb = new RadioButton(this);
//2.2.0        		if(ActivityUtility.getDPI(CartEditActivity.this).equals("240")){
//        			rb.setPadding(15, 0, 0, 0);
//        		}else if(ActivityUtility.getDPI(CartEditActivity.this).equals("160")){
//        			rb.setPadding(18, 0, 0, 0);
//        		}
				rb.setText(ladu);
				rb.setButtonDrawable(getResources().getDrawable(R.drawable.bg_singlechoose));
				laduRp.addView(rb);
				
				if(type.equals("dish")){
				if(dish!=null &&  dish.getPungentLevel()!=null && ladu.equals(dish.getPungentLevel())){
					laduRp.check(rb.getId());
					ladu_value = i;
				}
				}else if(type.equals("bill")){
					if(punLevel!=null && punLevel.contains(String.valueOf(i))){
						rb.setChecked(true);
						ladu_value = i;
					}
				}else if(appBillNotes!=null &&  appBillNotes.getPungentLevel()!=null && appBillNotes.getPungentLevel().contains(String.valueOf(i))){
					rb.setChecked(true);
					ladu_value = i;
				}
			}

			laduRp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					for(int index=0;index<group.getChildCount();index++){
						RadioButton checkedRb = (RadioButton) group.getChildAt(index);
						if(checkedRb.getId()==checkedId){
							ladu_value = index;
							break;
						}
					}

				}
			});
		}
		
	}
	
}

